package com.ly.system.controller;

import com.ly.common.core.bean.R;
import com.ly.common.core.constant.BaseCacheConstant;
import com.ly.common.core.utils.excel.ExcelResult;
import com.ly.common.core.utils.excel.ExcelUtil;
import com.ly.system.system.bean.dto.DeptTreeSearchDTO;
import com.ly.system.system.bean.entity.SysDept;
import com.ly.common.core.bean.vo.TreeVO;
import com.ly.system.service.ISysDeptService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 部门
 *
 * @author jeecg
 */
@Tag(name = "系统部门维护")
@Slf4j
@RestController
@RequestMapping("/sys/sysDept")
@RequiredArgsConstructor
public class SysDeptController {

    private final ISysDeptService sysDepartService;

    /**
     * 查询-id
     *
     * @param id
     * @return
     */
    @Operation(summary = "查询-id")
    @GetMapping("/getId")
    public R<SysDept> getId(@RequestParam(name = "id") String id) {
        return sysDepartService.getId(id);
    }

    /**
     * 新增
     *
     * @param entity
     * @return
     */
    @PostMapping("/add")
    @CacheEvict(value = {BaseCacheConstant.SYS_1DEPARTS_CACHE, BaseCacheConstant.SYS_1DEPART_IDS_CACHE}, allEntries = true)
    public R<String> add(@RequestBody SysDept entity) {
        return sysDepartService.add(entity);
    }

    /**
     * 修改
     *
     * @param entity
     * @return
     */
    @PutMapping("/edit")
    @CacheEvict(value = {BaseCacheConstant.SYS_1DEPARTS_CACHE, BaseCacheConstant.SYS_1DEPART_IDS_CACHE}, allEntries = true)
    public R<String> edit(@RequestBody SysDept entity) {
        return sysDepartService.edit(entity);
    }

    /**
     * 删除-id
     *
     * @param id
     * @return
     */
    @CacheEvict(value = {BaseCacheConstant.SYS_1DEPARTS_CACHE, BaseCacheConstant.SYS_1DEPART_IDS_CACHE}, allEntries = true)
    @DeleteMapping("/delete")
    public R<String> delete(@RequestParam(name = "id") String id) {
        return sysDepartService.delete(id);
    }

    /**
     * 批量删除 根据前端请求的多个ID,对数据库执行删除相关部门数据的操作
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteBatch")
    @CacheEvict(value = {BaseCacheConstant.SYS_1DEPARTS_CACHE, BaseCacheConstant.SYS_1DEPART_IDS_CACHE}, allEntries = true)
    public R<String> deleteBatch(@RequestParam(name = "ids") String ids) {
        return sysDepartService.deleteBatch(ids);
    }

    /**
     * 导出excel
     *
     * @param response
     */
    @Operation(summary = "导出excel")
    @GetMapping("/exportXls")
    public void exportXls(HttpServletResponse response) {
        List<SysDept> list = sysDepartService.list();
        ExcelUtil.exportExcel(list, "部门表", SysDept.class, response);
    }

    /**
     * 导入excel
     *
     * @param file
     * @param updateSupport：是否删除标识
     * @return
     */
    @Operation(summary = "导入excel")
    @PostMapping("/importExcel")
    public R<?> importWork(MultipartFile file, boolean updateSupport) throws Exception {
        // 监听： new ImportListener(updateSupport);
        ExcelResult<SysDept> result = ExcelUtil.importExcel(file.getInputStream(), SysDept.class, null);
        return R.ok(true, result.getAnalysis());
    }

    /**
     * 组织树
     *
     * @param dto
     * @return
     */
    @GetMapping("/getDeptTree")
    public R<List<TreeVO>> getDeptTree(DeptTreeSearchDTO dto) {
        return sysDepartService.getDeptTree(dto);
    }

    /**
     * 组织树-异步
     *
     * @param dto
     * @return
     */
    @GetMapping("/getDeptTreeSync")
    public R<List<TreeVO>> getDeptTreeSync(DeptTreeSearchDTO dto) {
        return sysDepartService.getDeptTreeSync(dto);
    }

    /**
     * 组织树-ids
     * @param ids
     * @return
     */
    @GetMapping("/getDeptTreeVoByIds")
    public R<List<TreeVO>> getDeptTreeVoByIds(@RequestParam(name = "ids") String ids) {
        return sysDepartService.getDeptTreeVoByIds(ids);
    }
}
